<template>
  <a-modal
    title="选择考评内容"
    style="top: 100px"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
  >
    <!-- 记差错 -->
    <a-card :bordered="false" style="height:450px;overflow-y:auto">
      <a-table
        :columns="deductionColumns"
        :data-source="deductionLoadData"
        :rowKey="row => row.id"
        :pagination="false"
        :loading="loading"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="openDeductionGrade(record)">评分</a>
        </span>
      </a-table>
    </a-card>
    <!-- 弹出框 -->
    <person-score-manage-grade ref="personScoreManageGrade" @updateDqdf="updateDqdf"></person-score-manage-grade>
  </a-modal>

</template>

<script>
import { STable } from '@/components'
import { query } from '@/api/admin'
import PersonScoreManageGrade from '@/views/par/personScoreManage/PersonScoreManageGrade'
export default {
  name: 'PersonScoreManageDeduction',
  components: {
    STable,
    PersonScoreManageGrade
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      deductionLoadData: [],
      // 表头
      deductionColumns: [
        { title: '考评标准及要点大类', dataIndex: 'dl' },
        { title: '考评内容', dataIndex: 'kpxName' },
        { title: '考评总分', dataIndex: 'kpfs' },
        { title: '当前得分',
          dataIndex: 'dqdf',
          customRender: function (attr1, record) {
            if (attr1 === undefined) {
              return record.kpfs
            } else {
              return attr1
            }
          } },
        { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }
        }
      ],
      loading: false
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {

  },
  methods: {
    /**
     * 记差错
     */
    deduction (record) {
      this.deductionLoadData = record.itemList
      this.visible = true
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleOk () {
      this.close()
    },
    handleCancel () {
      this.close()
    },
    /**
     * 打开记差错评分框
     */
    openDeductionGrade (record) {
      this.$refs.personScoreManageGrade.deduction(record)
    },
    updateDqdf (val, id) {
      this.loading = true
      this.deductionLoadData.some(item => {
        if (item.id === id) {
          if (val !== undefined && val.length > 0) {
            let num = Number(item.kpfs)
            val.map(it => {
              num = num + Number(it.pf)
            })
            item.dqdf = num
          } else {
            item.dqdf = 0
          }
        }
      })
      this.loading = false
    }

  }
}
</script>
